<!DOCTYPE html>
<html lang="zh_CN"
      xmlns:th="http://www.thymeleaf.org"
>
<head>
    <!--公用头部-->
    <th:block th:replace="common/head::commonHead('目录管理-小邑')"/>
    <style>
        #formAdvForm .layui-row {
            /*margin-top: 20px;*/
        }

        #formAdvForm .layui-form-item {
            margin-bottom: 0;
        }

        #formAdvForm .layui-form-item .layui-inline {
            margin-bottom: 25px;
            margin-right: 0;
        }
    </style>
</head>
<!--公用js-->
<th:block th:replace="common/head::commonJs"/>
<script>
    var el = null;
    var filePath = null;
</script>
<body marginwidth="0" marginheight="0">
<form class="layui-form" action="" id="formAdvForm" lay-filter="formAdvForm">
    <div class="layui-fluid  layui-fluid-btn">
        <div class="layui-col-md12">
            <div class="layui-card" style="margin: 10px;">
                <div class="layui-card-body">
                    <input name="id" type="text" style="display: none">
                    <div class="layui-form-item layui-row">
                        <div class="layui-inline">
                            <div class="layui-input-inline">
                                <input type="text" name="fileName" id="fileName"  placeholder="文件名" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                                <button class="xy-btn xy-btn-primary xy-btn-md" onclick="searchReload()" type="button"><i class="layui-icon">&#xe615;</i> 搜索</button>
                                <button class="xy-btn xy-btn-md" onclick="resetTree()" type="button"><i class="layui-icon">&#xe669;</i> 重置</button>
                        </div>
                    </div>
                    <div class="layui-form-item ">
                        <div class="layui-inline layui-col-md12" id="authTree"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<script  th:inline="javascript">
    layui.use(['layer', 'jquery', 'form', 'notice', 'eleTree'], function () {
        var $ = layui.jquery
            , form = layui.form
            , notice = layui.notice
            , eleTree = layui.eleTree
            , layer = layui.layer;
        el = eleTree({
            el: '#authTree',
            highlightCurrent: true,
            // showCheckbox: true,
            isDefaultChangePstatus: true,
            //checkStrictly: true,
            url: basePath + '/file/getFileTree',
            where: {
                parentId: 0
            },
            imgUrl: basePath + "/static/admin/js/layui/3lib/eleTree/images/",
            icon: {
                dropdownOff: "dropdownOff.png",
                dropdownOn: "dropdownOn.png",
                checkFull: "checkFull.png",
                checkHalf: "checkHalf.png",
                checkNone: "checkNone.png",
                fold: "fold.png",
                leaf: "leaf.png",
                loading: ".eleTree-animate-rotate.eletree_icon-loading1",
            },
            rightMenuList: function(data) {
                if(data.filedName1 == 0) {
                    return [{name: "下载", value: "download"},{name: "重命名", value: "rename"}, {name: "删除", value: "delete"}];
                }
                return [{name: "重命名", value: "rename"}, {name: "删除", value: "delete"}];
            }
        });
        /*右键菜单  踩坑 自定义的项事件名为custom_加上value值*/
        el.on("custom_download", function(data) {
            setTimeout(()=>{
                window.location.href = basePath+'/downloadFile?filePath='+data.data.filedName4+'&fileName='+data.data.label;
                data.load()
                } , 100)
        }).on("custom_rename", function(data) {
            setTimeout(()=>{
                if (data.data.filedName1 == 1) {
                    filePath = data.data.filedName4.substring(0,data.data.filedName4.lastIndexOf("/"));
                    // 文件夹
                    layer.open({
                        type: 2,
                        id: "addFolder",
                        skin: 'layer-ext-myskin',
                        title: "文件夹重命名",
                        shadeClose: false,
                        shade: 0.3,
                        area: ["460px", "245px"],
                        content: basePath + "/view/system/file/file_add_update_folder?type=2&parentId=" + data.data.pid + "&id=" + data.data.id,
                        end: function () {
                            resetTree();
                        }
                    });
                } else {
                    filePath = "";
                    //文件
                    layer.open({
                        type: 2,
                        id: "updateFile",
                        skin: 'layer-ext-myskin',
                        title: "文件重命名",
                        shadeClose: false,
                        shade: 0.3,
                        area: ["460px", "245px"],
                        content: basePath + "/view/system/file/file_add_update?parentId="  + data.data.pid + "&id=" + data.data.id,
                        end: function () {
                            resetTree();
                        }
                    });
                }
                data.load();
            }, 100)
        }).on("custom_delete", function(data) {
            setTimeout(()=>{
                layer.confirm('确定要删除吗？', {icon: 5, title: '提示', skin: 'layer-ext-myskin'}, function (index) {
                    layer.close(index);
                    // 异步请求
                    sendAjaxPostToError(
                        basePath + '/file/delete',
                        {
                            id: data.data.id
                        },
                        function (result) {
                            if (result.success) {
                                top.layui.notice.msg(result.msg ,{icon: 1,position:"topCenter"});
                                resetTree();
                            } else {
                                top.layui.notice.msg(result.msg ,{icon: 2,position:"topCenter"});
                            }
                        },
                        function (data) {
                            if (utils.string.isEmpty(data) && utils.string.isEmpty(data.msg)){
                                top.layui.notice.msg(data.msg ,{icon: 2,position:"topCenter"});
                            }else{
                                top.layui.notice.msg("服务器内部错误，请联系管理员~", {icon: 2,position:"topCenter"});
                            }
                        }
                    )
                });
                data.load();
            }, 100)
        })
    });
    /*搜索*/
    function searchReload(){
        el.search($("#fileName").val(), function(value, data) {
            if (!value) return true;
            return data.label.indexOf(value) !== -1;
        })
    }
    /*重置*/
    function resetTree(){
        $("#fileName").val("");
        el.reload();
    }
</script>
</body>
</html>
